package com.google.gwt.livinglife.client.widgets;

import java.util.Iterator;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.livinglife.client.LivingLife;
import com.google.gwt.livinglife.client.data.ChallengeData;
import com.google.gwt.livinglife.client.data.ResponseData;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ChallengeBox extends Composite {
	private DecoratorPanel decorator_panel = new DecoratorPanel();
	private VerticalPanel main_panel = new VerticalPanel();
	Button like_button = new Button("like");
	Button inspired_button = new Button("inspired");
	Button dislike_button = new Button("dislike");
	Button respond_button = new Button("respond");
	Label num_response_label = new Label();
	
	FlowPanel body = new FlowPanel();
	Anchor author_link = new Anchor();
	HTML text = new HTML();
	VerticalPanel picture_container = new VerticalPanel();
	Image picture = new Image();
	Boolean response_form_added = false;
	ResponseEntryForm response_form = new ResponseEntryForm();
	Label date_label = new Label();
	private HorizontalPanel  buttons_bar = new HorizontalPanel();
	HorizontalPanel responses_horizontal_container = new HorizontalPanel();
	Label responses_padding = new Label("...");
	VerticalPanel response_container = new VerticalPanel();
	int response_start_index = 0;
	int response_end_index = 1000;  // Very large number.
	
	private ChallengeData challenge_data;
	
	public ChallengeBox() {
		decorator_panel.setWidget(main_panel);
		main_panel.add(body);
		main_panel.add(picture_container);
		main_panel.add(buttons_bar );
		main_panel.add(responses_horizontal_container);
		responses_horizontal_container.add(responses_padding);
		responses_horizontal_container.add(response_container);
		body.add(author_link);
		body.add(text);
		buttons_bar.add(date_label);
		buttons_bar.add(respond_button);
		buttons_bar.add(inspired_button);
		buttons_bar.add(like_button);
		buttons_bar.add(dislike_button);
		buttons_bar.add(num_response_label);
		
		response_form.setParent_box(this);
		
		
		// add all the clickHandlers.
		respond_button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (response_form_added == false) {
					response_form_added = true;		
					main_panel.add(response_form);
				}
			}
		});
		
		inspired_button.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
			}
		});
		initWidget(decorator_panel);
	}

	public ChallengeData getChallenge_data() {
		return challenge_data;
	}

	public void setChallenge_data(ChallengeData challengeData) {
		challenge_data = challengeData;
		text.setHTML(challenge_data.getPost_text());
		author_link.setHTML(challenge_data.getAuthor().getNickname());
		author_link.setHref(LivingLife.getUserIdUrl(challenge_data.getAuthor().getId()));
		if (challenge_data.getEmbeded_image() != null && !(challenge_data.getEmbeded_image().length() == 0)) {
			picture.setUrl(challenge_data.getEmbeded_image());
			picture_container.add(picture);
			picture.setHeight("4in");
		} else {
			picture_container.clear();
		}
		response_form.setChallenge_id(challenge_data.getId());
		
		date_label.setText(DateTimeFormat.getMediumDateFormat().format(challenge_data.getCreation_date()) + "  ");
		Iterator<ResponseData> i = challenge_data.getResponses().iterator();
		int index = 0;
		response_container.clear();
		while(i.hasNext()) {
			if (index >= response_start_index && index < response_end_index) {
			  ResponseData r = i.next();
			  ResponseBox rb = new ResponseBox();
			  rb.setResponse_data(r);
			  response_container.add(rb);
			}
			index++;
		}
		num_response_label.setText(challenge_data.getNum_responses() + " responses");
	}
	public void AddResponseData(ResponseData rd) {
		challenge_data.getResponses().add(rd);
		ResponseBox rb = new ResponseBox();
		rb.setResponse_data(rd);
		response_container.add(rb);
		challenge_data.setNum_responses(challenge_data.getNum_responses() + 1);
		num_response_label.setText(challenge_data.getNum_responses() + " responses");
	}
	
	public void removeResponseForm() {
		response_form_added = false;
		main_panel.remove(response_form);
	}

	public void setResponseDisplayRange(int start, int end) {
		response_start_index = start;
		response_end_index = end;
	}
}
